﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Windows;

namespace Microsoft.WPF.Samples.CoverFlow
{
    /// <summary>
    /// The BizCardFlow is used as hosting container for WPF apps.
    /// </summary>
    public partial class BizCardFlow 
        : INotifyPropertyChanged
    {
        // Events
 
        #region SelectedItemChanged
        private event EventHandler _selectedItemChanged;

        private static readonly object s_selectedItemChangedLock =
            new object();

        public event EventHandler SelectedItemChanged
        {
            add
            {
                lock (s_selectedItemChangedLock)
                {
                    _selectedItemChanged += value;
                }
            }
            remove
            {
                lock (s_selectedItemChangedLock)
                {
                    _selectedItemChanged -= value;
                }
            }
        }

        private void OnCoverFlowListSelectedItemChanged(object sender,EventArgs e)
        {
            if (_selectedItemChanged != null)
            {
                _selectedItemChanged(sender, e);
            }
            OnPropertyChanged("SelectedItem");
        }

        private void OnCoverFlowListSelectedItemChanged()
        {
            OnCoverFlowListSelectedItemChanged(this, EventArgs.Empty);
        }
        #endregion

        #region PropertyChanged
        private event PropertyChangedEventHandler _propertyChanged;
        private static readonly object s_propertyChangedLock = new object();

        /// <summary>
        /// Occurs when a property changed.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged
        {
            add
            {
                lock (s_propertyChangedLock)
                {
                    _propertyChanged += value;
                }
            }
            remove
            {
                lock (s_propertyChangedLock)
                {
                    _propertyChanged -= value;
                }
            }
        }

        protected void OnPropertyChanged(string name)
        {
            if(_propertyChanged!=null)
            {
                _propertyChanged(
                    this,
                    new PropertyChangedEventArgs(name));
            }
        }
        #endregion


        // Constructor

        #region BizCardFlow()
        /// <summary>
        /// Initializes a new instance of the <see cref="BizCardFlow"/> class.
        /// </summary>
        public BizCardFlow()
        {
            try
            {
                InitializeComponent();
                CoverFlowList.SelectedItemChanged +=
                    OnCoverFlowListSelectedItemChanged;
            }
            // ReSharper disable EmptyGeneralCatchClause
            catch (Exception)
            // ReSharper restore EmptyGeneralCatchClause
            {
                // Catch exceptions from designer...
            }
        }
        #endregion


        // Properties

        #region ItemsSource
        /// <summary>
        /// Gets or sets the items source.
        /// </summary>
        /// <value>The items source.</value>
        public IEnumerable ItemsSource
        {
            get { return CoverFlowList.ItemsSource; }
            set
            {
                CoverFlowList.ItemsSource = value;
                
                // For interop ensure rendering is done after binding...
                if (Application.Current == null)
                {
                    CoverFlowList.RegenerateItems();
                }
                OnCoverFlowListSelectedItemChanged();
            }
        }
        #endregion

        #region SelectedIndex
        /// <summary>
        /// Gets or sets the index of the selected.
        /// </summary>
        /// <value>The index of the selected.</value>
        public int SelectedIndex
        {
            get { return CoverFlowList.SelectedIndex; }
            set
            {
                CoverFlowList.SelectedIndex = value;
                OnCoverFlowListSelectedItemChanged();
            }
        }
        #endregion

        #region SelectedItem
        /// <summary>
        /// Gets the selected item.
        /// </summary>
        /// <value>The selected item.</value>
        public object SelectedItem
        {
            get { return CoverFlowList.SelectedItem; }
        } 
        #endregion


        // Methods

        #region Slider_ValueChanged()
        /// <summary>
        /// Slider_s the value changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="double"/> instance containing the 
        /// event data.</param>
        /// <remarks>Let </remarks>
        private void Slider_ValueChanged( object sender,RoutedPropertyChangedEventArgs<double> e)
        {
            OnCoverFlowListSelectedItemChanged();
        }
        
        #endregion

    }
}
